package com.mind.jenkins.pipeline.utils

import groovy.json.JsonOutput

class BuildConfig implements Serializable {
    static final String TYPE_APP = "app"
    static final String TYPE_LIB = "lib"

    private Object config
    private Object script

    private def defaultMavenDeploy = [
            [branch: "master", suffix: "-SNAPSHOT"],
            [branch: "online", suffix: ""],
            [branch: "release", suffix: ""],
            [branch: "2.x", suffix: ""]
    ]

    private def gitLabMavenDeploy = [
            [branch: "master", suffix: ""]
    ]

    BuildConfig(config, script) {
        this.config = config
        this.script = script
    }

    static BuildConfig load(script) {
        Object config = null
        try {
            config = script.readYaml file: './build.yaml'
        } catch (err) {
            script.echo("读取 build.yaml 文件失败")
            script.echo(err.message)
            throw err
        }
        return new BuildConfig(config, script)
    }

    boolean isAppRepo() {
        try {
            return TYPE_APP.equals(config?.type) || config?.type.contains(TYPE_APP)
        } catch (e) {
            script.echo(e.message)
            script.echo(JsonOutput.toJson(config))
        }
        return false
    }

    boolean isLibRepo() {
        try {
            return TYPE_LIB.equals(config?.type) || config?.type.contains(TYPE_LIB)
        } catch (e) {
            script.echo(e.message)
            script.echo(JsonOutput.toJson(config))
        }
        return false
    }

    List<Object> getApps() {
        return config?.apps
    }

    Object getAppSetting(String appname) {
        if (config.apps == null) {
            return null
        }

        for (Object app : config.apps) {
            if (app.name == appname) {
                return app
            }
        }
        return null
    }

    Object getNotifyConfig() {
        return config?.notify
    }

    List<Object> getLibs() {
        if (config?.libs == null || config?.libs.size() == 0) {
            return null
        }
        return config?.libs.join(",")
    }

    List<Object> getMavenDeployConfig() {
        return config?.mavenDeploy == null ? defaultMavenDeploy : config?.mavenDeploy
    }

    List<Object> getGitLabDeployConfig() {
        return config?.mavenDeploy == null ? gitLabMavenDeploy : config?.mavenDeploy
    }

    String getSuffix(branchName) {
        return findSuffix(getMavenDeployConfig(), branchName)
    }

    String getGitLabSuffix(branchName) {
        return findSuffix(getGitLabDeployConfig(), branchName)
    }

    String findSuffix(List<Object> configList, String branchName) {
        for (Object branchConfig : configList) {
            if (branchConfig.branch == branchName) {
                // if exist branchName property, return empty string as default
                return branchConfig.suffix == null ? "" : branchConfig.suffix
            }
        }
        return null
    }

    Object getCustomConfig() {
        return config?.config
    }


    /**
     * 是否允许功能分支更新时打 tag，默认不打 tag
     *
     * @return
     */
    boolean enableFeatureBranchTag() {
        def customConfig = getCustomConfig()
        if (customConfig == null) {
            return false
        }
        return customConfig.enableFeatureBranchTag
    }

    /**
     * 是否禁止功能分支发布 maven ，默认会发布 maven
     *
     * @return
     */
    boolean disableFeatureBranchDeploy() {
        def customConfig = getCustomConfig()
        if (customConfig == null) {
            return false
        }
        return customConfig.disableFeatureBranchDeploy
    }
}